package com.dinglian.ots.service.impl;

import cn.hutool.core.date.DateUtil;
import com.dinglian.ots.dao.mapper.BrandMapper;
import com.dinglian.ots.dao.mapper.ProductMapper;
import com.dinglian.ots.dao.mapper.StoreMapper;
import com.dinglian.ots.dao.model.*;
import com.dinglian.ots.service.AdminService;
import com.dinglian.ots.vo.brand.BrandRankVo;
import com.dinglian.ots.vo.buyer.ExaminationBuyerVo;
import com.dinglian.ots.vo.product.ProductRankVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangbo
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo<ExaminationBuyerVo> getBuyerExaminationList(String name, String phone, Integer status, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ExaminationBuyerVo> buyerExaminationList =storeMapper.getBuyerExaminationList(name,phone,status,startTime,endTime);
        return new PageInfo<>(buyerExaminationList);
    }


    @Override
    public void examineBuyer(Integer buyerId, Integer status) {
        Store store = new Store();
        store.setId(buyerId);
        store.setStatus(status);
        store.setAuditTime(DateUtil.date());
        storeMapper.updateByPrimaryKeySelective(store);
    }

    @Override
    public Store getBuyerInfo(Integer buyerId) {
        return storeMapper.selectByPrimaryKey(buyerId);
    }

    @Override
    public void batchExamineBuyer(Integer[] ids, Integer status) {
        List<Integer> idList = Arrays.asList(ids);
        Store store = new Store();
        store.setStatus(status);
        store.setAuditTime(DateUtil.date());

        StoreExample storeExample = new StoreExample();
        storeExample.or().andIdIn(idList);
        storeMapper.updateByExampleSelective(store,storeExample);

    }

    @Override
    public PageInfo<BrandRankVo> getBrandRankList(String name, String phone,Integer isRecommend, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BrandRankVo> brandRankVos = brandMapper.getBrandRankList(name,phone,isRecommend,startTime,endTime);
        return new PageInfo<>(brandRankVos);
    }

    @Override
    public void recommendBrand(Integer id, Boolean isRecommend) {
        BrandWithBLOBs brand = new BrandWithBLOBs();
        brand.setId(id);
        brand.setIsRecommend(isRecommend);
        brandMapper.updateByPrimaryKeySelective(brand);
    }


    @Override
    public void editBrandSort(Integer id, Integer sort) {
        BrandWithBLOBs brand = new BrandWithBLOBs();
        brand.setId(id);
        brand.setSort(sort);
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void batchRecommend(Integer[] ids, Boolean isRecommend) {
        List<Integer> idList = Arrays.asList(ids);
        BrandWithBLOBs brand =new BrandWithBLOBs();
        brand.setIsRecommend(isRecommend);
        BrandExample brandExample = new BrandExample();
        brandExample.or().andIdIn(idList);
        brandMapper.updateByExampleSelective(brand,brandExample);
    }

    @Override
    public PageInfo<ProductRankVo> getProductRankList(String brandName, String title, String styleName, Integer isRecommend, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductRankVo> productRankVos = productMapper.getProductRankList(brandName,title,styleName,isRecommend,startTime,endTime);
        return new PageInfo<>(productRankVos);
    }

    @Override
    public void recommendProduct(Integer id, Boolean isRecommend) {
        Product product = new Product();
        product.setId(id);
        product.setIsRecommend(isRecommend);
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void editProductSort(Integer id, Integer sort) {
        Product product = new Product();
        product.setId(id);
        product.setSort(sort);
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void batchRecommendProduct(Integer[] ids, Boolean isRecommend) {
        List<Integer> idList = Arrays.asList(ids);
        Product product = new Product();
        product.setIsRecommend(isRecommend);
        ProductExample productExample = new ProductExample();
        productExample.or().andIdIn(idList);
        productMapper.updateByExampleSelective(product,productExample);
    }

    @Override
    public void batchShowBrand(Integer[] ids, Boolean isShow) {
        List<Integer> idList = Arrays.asList(ids);
        BrandWithBLOBs brand =new BrandWithBLOBs();
        brand.setIsShow(isShow);
        BrandExample brandExample = new BrandExample();
        brandExample.or().andIdIn(idList);
        brandMapper.updateByExampleSelective(brand,brandExample);
    }


    @Override
    public PageInfo<BrandRankVo> getTopBrandRankList(String name, String phone, Integer isRecommend, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BrandRankVo> brandRankVos = brandMapper.getTopBrandRankList(name,phone,isRecommend,startTime,endTime);
        return new PageInfo<>(brandRankVos);
    }

    @Override
    public void editTopBrandSort(Integer id, Integer sort) {
        BrandWithBLOBs brand = new BrandWithBLOBs();
        brand.setId(id);
        brand.setTopSort(sort);
        brandMapper.updateByPrimaryKeySelective(brand);
    }
}
